home *** CD-ROM | disk | FTP | other *** search
- /*
- udp test translated into rexx
- from Amiga Magazine n.93 udp.c test by Rudi Chiarito
-
- Show the use of udp protocol.
- Usage: udp <host> <port> <localPort>
-
- i.e.:
- shell1 udp localhost 4000 4001
- shell2 udp localhost 4001 4000
-
- to stop push <ctrl-c> in the shells.
-
- To see how rx/tx is stopped just write something in one or both
- the shells.
-
- */
-
- parse arg host remotePort localPort .
-
- /**START libraries*/
- if ~show("L","rexxsupport.library") then
- if ~addlib("rexxsupport.library",0,-30) then do
- say "no rexxsupport.library"
- exit
- end
- if ~show("L","rxsocket.library") then
- if ~addlib("rxsocket.library",0,-30) then do
- say "no rxsocket.library"
- exit
- end
- if ~show("L","rmh.library") then
- if ~addlib("rmh.library",0,-30) then do
- say "no rmh.library"
- exit
- end
- /**END libraries**/
-
- if IsDotAddr(host) then do
- remote.addrFamily = "INET"
- remote.addrAddr = host
- end
- else do
- if ~gethostbyname("H",host) then do
- say "udp: host" host "not found."
- exit
- end
- remote.addrFamily = h.hostAddrType
- remote.addrAddr = h.hostAddrList.0
- end
-
- remote.addrPort = remotePort
-
- sock = socket("INET","DGRAM","IP")
- if sock<0 then do
- say "udp: can't create socket:" errno()
- exit
- end
-
- local.addrFamily = "INET"
- local.addrAddr = 0
- local.addrPort = localPort
- if bind(sock,"LOCAL")<0 then do
- say "udp: can't bind port:" errno()
- exit
- end
-
- call IOCtlSocket(sock,"FIONBIO",1)
-
- data = "Ciao"
- dataLen = length(data)
-
- down = 0
- do while 1
-
- n = SendTo(sock,data,0,"REMOTE")
- if n!=dataLen then do
- say "udp SendTo() error:" errno()
- exit
- end
-
- call delay(10)
-
- if down then call WriteCh("STDOUT",".")
-
- n = RecvFrom(sock,"BUFF",10,0,"REMOTE")
- if n==-1 then do
- err = errno()
- if err==35 then do
- if ~down then do
- call WriteCh("STDOUT","udp: no more data.")
- down = 1
- end
- end
- else do
- say "udp: RecvFrom() error:" err
- exit
- end
- end
- else do
- say "udp: read byte(s):" n
- down = 0
- end
-
- end
-